package com.czy.owner.ui.login;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.czy.owner.R;
import com.czy.owner.adapter.LoginPagerAdapter;
import com.czy.owner.global.Constants;
import com.czy.owner.ui.BaseActivity;
import com.czy.owner.ui.MainActivity;
import com.czy.owner.ui.hotshop.GoodsDetailActivity;
import com.czy.owner.utils.ActivityHelper;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by yang on 2017/9/26.
 */

public class LoginActivity extends BaseActivity {
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.top_rg_a)
    RadioButton topRgA;
    @BindView(R.id.top_rg_b)
    RadioButton topRgB;
    @BindView(R.id.main_top_rg)
    RadioGroup mainTopRg;
    @BindView(R.id.main_viewpager)
    ViewPager mainViewpager;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_login_radiobutton;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        if (getIntent().getExtras() == null || getIntent().getExtras().getBoolean(Constants.TRANSMISSION_CONTENT_KEY, true)) {
            ActivityHelper.getInstance().keepLastActivity();
        }

        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!MainActivity.isMainActivity){
                    startActivity(new Intent(LoginActivity.this,MainActivity.class));
                }
                finish();
            }
        });
        getToolbarTitle().setText("登录");
        initEvent();
    }

    @Override
    protected void updateViews() {

    }

    private void initEvent() {
        List<Fragment> fragments = new ArrayList<>();
        fragments.add(new LoginBySmsFragment());
        fragments.add(new AccountLoginFragment());

        LoginPagerAdapter pagerAdapter = new LoginPagerAdapter(getSupportFragmentManager(), fragments);
        mainViewpager.setAdapter(pagerAdapter);
        mainViewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 0) {
                    topRgA.setChecked(true);
                } else {
                    topRgB.setChecked(true);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        mainTopRg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == topRgA.getId()) {
                    mainViewpager.setCurrentItem(0);
                } else if (checkedId == topRgB.getId()) {
                    mainViewpager.setCurrentItem(1);
                }

            }
        });

        //设置默认页
        mainViewpager.setCurrentItem(0);
    }


}
